package com.study_platform.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.study_platform.entity.Point;
import com.study_platform.entity.Question;
import com.study_platform.service.PointService;
import com.study_platform.service.QuestionService;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/study_platform/point")
public class PointController {

    @Resource
    private PointService pointService;
    @Resource
    private QuestionService questionService;

    @GetMapping("/getContent/{point}/")
    public String getContent(@PathVariable String point){
        Point point1 = pointService.lambdaQuery()
                .eq(Point::getPoint, point)
                .one();
        return point1.getContent();
    }

    @GetMapping("/getByGrade/")
    public JSONObject get(@RequestParam String grade){
        List<Question> questions = questionService.lambdaQuery()
                .eq(Question::getGrade, grade)
                .list();
        Set<String> set = new HashSet<>();
        questions.forEach(question -> {
            set.add(question.getPoint());
        });
        List<Point> points = new ArrayList<>();
        set.forEach(point -> {
            List<Point> list = pointService.lambdaQuery()
                    .eq(Point::getPoint, point)
                    .list();
            points.addAll(list);
        });
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("points", points);
        return jsonObject;
    }

    @GetMapping("/getNumber/")
    public Integer getNum(@RequestParam String point){
        return questionService.lambdaQuery()
                .eq(Question::getPoint, point)
                .count();
    }
}
